#include<iostream>
#include<string.h>
using std::endl;
using std::cout;

class String{

public:
    String(const char*pstr)
    :_pstr(new char[strlen(pstr)+1]()){
        cout<<"string(const char*pstr)"<<endl;
        strcpy(_pstr,pstr);
    }
    //拷贝构造函数
    String(const String&rhs);
    //移动构造函数
    String(String&&rhs);
    //赋值运算符函数
    String&operator= (const String&rhs);
    //移动赋值运算符函数
    String&operator=(String&&rhs);
    //析构
    ~String(){
        cout<<"~String"<<endl;
        if(_pstr){
            delete [] _pstr;
            _pstr = nullptr;
        }
    }

private:
    int size;
    char*_pstr;
};
String::String(const String&rhs)
:_pstr(new char[strlen(rhs._pstr)]())
{
    cout<<"String(const String &rhs)"<<endl;
    strcpy(_pstr,rhs._pstr);
}
String::String(String&&rhs)
:_pstr(rhs._pstr)
{
    cout<<"String(String&&rhs)"<<endl;
    //避免double free
    rhs._pstr = nullptr;
}
String& String::operator=(const String&rhs){
    //判断自复制
    if(this!=&rhs){
        delete [] _pstr;
        _pstr = new char[strlen(rhs._pstr)]();
        strcpy(_pstr,rhs._pstr);
    }
    return *this;
}
String&String::operator=(String&&rhs){
    //还是需要判断自复制，因为std::move
    if(this!=&rhs){
        delete[]_pstr;
        _pstr=rhs._pstr;
        rhs._pstr = nullptr;
        cout<<"String::operator=(String&&rhs)"<<endl;
    }
    return *this;
}

void test0(){
    String s1 = "hello";
    s1= String("word");
    //s1 = s2;
    String s2 = s1;
}
int main(void){
    test0();
}